var _typeof2 = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(e) {
    return typeof e;
} : function(e) {
    return e && "function" == typeof Symbol && e.constructor === Symbol && e !== Symbol.prototype ? "symbol" : typeof e;
}, _typeof = "function" == typeof Symbol && "symbol" == _typeof2(Symbol.iterator) ? function(e) {
    return void 0 === e ? "undefined" : _typeof2(e);
} : function(e) {
    return e && "function" == typeof Symbol && e.constructor === Symbol && e !== Symbol.prototype ? "symbol" : void 0 === e ? "undefined" : _typeof2(e);
}, _base = require("base64"), _md = require("md5"), _md2 = _interopRequireDefault(_md);

function _interopRequireDefault(e) {
    return e && e.__esModule ? e : {
        default: e
    };
}

var util = {};

function getQuery(e) {
    var t = [];
    if (-1 != e.indexOf("?")) for (var n = e.split("?")[1].split("&"), a = 0; a < n.length; a++) n[a].split("=")[0] && unescape(n[a].split("=")[1]) && (t[a] = {
        name: n[a].split("=")[0],
        value: unescape(n[a].split("=")[1])
    });
    return t;
}

function getUrlParam(e, t) {
    return t = new RegExp("(^|&)" + t + "=([^&]*)(&|$)"), null != (t = e.split("?")[1].match(t)) ? unescape(t[2]) : null;
}

function getSign(e, t, n) {
    var a = require("underscore.js"), r = require("md5.js"), o = "";
    if (getUrlParam(e, "sign") || t && t.sign) return !1;
    if (e && (o = getQuery(e)), t) {
        var i, s = [];
        for (i in t) i && t[i] && (s = s.concat({
            name: i,
            value: t[i]
        }));
        o = o.concat(s);
    }
    o = a.sortBy(o, "name"), o = a.uniq(o, !0, "name");
    for (var u = "", c = 0; c < o.length; c++) o[c] && o[c].name && o[c].value && (u += o[c].name + "=" + o[c].value, 
    c < o.length - 1 && (u += "&"));
    return r(u + (n = n || getApp().siteInfo.token));
}

util.base64Encode = function(e) {
    return (0, _base.base64_encode)(e);
}, util.base64Decode = function(e) {
    return (0, _base.base64_decode)(e);
}, util.b64_hmac_sha1 = function(e, t, o, i) {
    function m(e, t) {
        var n = (65535 & e) + (65535 & t);
        return (e >> 16) + (t >> 16) + (n >> 16) << 16 | 65535 & n;
    }
    function w(e, t) {
        return e << t | e >>> 32 - t;
    }
    function s(e, t) {
        e[t >> 5] |= 128 << 24 - t % 32, e[15 + (t + 64 >> 9 << 4)] = t;
        for (var n = [ 80 ], a = 1732584193, r = -271733879, o = -1732584194, i = 271733878, s = -1009589776, u = 0; u < e.length; u += 16) {
            for (var c = a, f = r, g = o, l = i, d = s, p = 0; p < 80; p++) {
                n[p] = p < 16 ? e[u + p] : w(n[p - 3] ^ n[p - 8] ^ n[p - 14] ^ n[p - 16], 1);
                var h = m(m(w(a, 5), (h = i, p < 20 ? r & o | ~r & h : !(p < 40) && p < 60 ? r & o | r & h | o & h : r ^ o ^ h)), m(m(s, n[p]), (h = p) < 20 ? 1518500249 : h < 40 ? 1859775393 : h < 60 ? -1894007588 : -899497514)), s = i, i = o, o = w(r, 30), r = a, a = h;
            }
            a = m(a, c), r = m(r, f), o = m(o, g), i = m(i, l), s = m(s, d);
        }
        return [ a, r, o, i, s ];
    }
    function u(e) {
        for (var t = [], n = (1 << i) - 1, a = 0; a < e.length * i; a += i) t[a >> 5] |= (e.charCodeAt(a / 8) & n) << 32 - i - a % 32;
        return t;
    }
    return o = o || "=", i = i || 8, function(e) {
        for (var t = "", n = 0; n < 4 * e.length; n += 3) for (var a = (e[n >> 2] >> 8 * (3 - n % 4) & 255) << 16 | (e[n + 1 >> 2] >> 8 * (3 - (n + 1) % 4) & 255) << 8 | e[n + 2 >> 2] >> 8 * (3 - (n + 2) % 4) & 255, r = 0; r < 4; r++) 8 * n + 6 * r > 32 * e.length ? t += o : t += "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt(a >> 6 * (3 - r) & 63);
        return t;
    }(function(e, t) {
        var n = u(e);
        16 < n.length && (n = s(n, e.length * i));
        for (var a = [ 16 ], r = [ 16 ], o = 0; o < 16; o++) a[o] = 909522486 ^ n[o], r[o] = 1549556828 ^ n[o];
        return t = s(a.concat(u(t)), 512 + t.length * i), s(r.concat(t), 672);
    }(e, t));
}, util.ksort = function(e) {
    var t, n = [];
    for (t in e) n.push(t);
    n.sort();
    for (var a = {}, r = 0; r < n.length; r++) {
        var o = n[r];
        a[o] = e[o];
    }
    return a;
}, util.md5 = function(e) {
    return (0, _md2.default)(e);
}, util.urlToJson = function(e) {
    for (var t = [], n = {}, a = (t = e.split("&")).length, r = 0; r < a; r++) n[t[r].split("=")[0]] = t[r].split("=")[1];
    return n;
}, util.JsonToUrl = function(e) {
    var t = "";
    if (0 < Object.keys(e).length) for (var n in t += "?", e) t += "&" + n + "=" + e[n];
    return t;
}, util.url = function(e, t) {
    var n = !(2 < arguments.length && void 0 !== arguments[2]) || arguments[2], a = getApp(), r = a.siteInfo.siteroot + "?i=" + a.siteInfo.uniacid + "&t=" + a.siteInfo.multiid + "&v=" + a.siteInfo.version + "&from=wxapp&";
    if (e && ((e = e.split("/"))[0] && (r += "c=" + e[0] + "&"), e[1] && (r += "a=" + e[1] + "&"), 
    e[2] && (r += "do=" + e[2] + "&")), t && "object" === (void 0 === t ? "undefined" : _typeof(t))) for (var o in t) o && t.hasOwnProperty(o) && t[o] && (r += o + "=" + t[o] + "&");
    return !0 === n && (r = r + "&state=we7sid-" + wx.getStorageSync("userInfo").sessionid, 
    (n = getCurrentPages()).length && (n = n[getCurrentPages().length - 1]) && n.__route__ && (r = r + "&m=" + n.__route__.split("/")[0]), 
    (n = getSign(r)) && (r = r + "&sign=" + n)), r;
}, util.getSign = function(e, t, n) {
    return getSign(e, t, n);
}, util.request = function(n) {
    require("underscore.js");
    var e = require("md5.js"), a = getApp();
    (n = n || {}).cachetime = n.cachetime || 0, n.showLoading = void 0 === n.showLoading || n.showLoading;
    var t = wx.getStorageSync("userInfo").sessionid, r = n.url;
    -1 == r.indexOf("http://") && -1 == r.indexOf("https://") && (r = util.url(r, "", !1)), 
    getUrlParam(r, "state") || n.data && n.data.state || !t || (r = r + "&state=we7sid-" + t), 
    n.data && n.data.m || (o = getCurrentPages()).length && (o = o[getCurrentPages().length - 1]) && o.__route__ && (r = r + "&m=" + o.__route__.split("/")[0]);
    var o = getSign(r, n.data);
    if (o && (r = r + "&sign=" + o), !r) return !1;
    if (wx.showNavigationBarLoading(), n.showLoading && util.showLoading(), n.cachetime) {
        var i = e(r), e = wx.getStorageSync(i), s = Date.parse(new Date());
        if (e && e.data) {
            if (e.expire > s) return n.complete && "function" == typeof n.complete && n.complete(e), 
            n.success && "function" == typeof n.success && n.success(e), console.log("cache:" + r), 
            wx.hideLoading(), wx.hideNavigationBarLoading(), !0;
            wx.removeStorageSync(i);
        }
    }
    n.data ? n.data.formIds = "" : n.data = {}, a.globalData.from_uid && (n.data.from_uid = a.globalData.from_uid), 
    a.globalData.from_act && (n.data.from_act = a.globalData.from_act), wx.request({
        url: r,
        data: n.data || {},
        header: n.header || {
            "content-type": "application/x-www-form-urlencoded"
        },
        method: n.method || "GET",
        success: function(e) {
            var t;
            wx.hideNavigationBarLoading(), !1 !== n.showLoading && wx.hideLoading(), e.data.errno ? "41009" == e.data.errno ? (wx.setStorageSync("userInfo", ""), 
            util.getUserInfo(function() {
                util.request(n);
            })) : n.fail && "function" == typeof n.fail ? n.fail(e) : e.data.message && (t = null != e.data.data && e.data.data.redirect ? e.data.data.redirect : "", 
            a.util.message(e.data.message, t, "error")) : (n.success && "function" == typeof n.success && n.success(e), 
            n.cachetime && (e = {
                data: e.data,
                expire: s + 1e3 * n.cachetime
            }, wx.setStorageSync(i, e)));
        },
        fail: function(e) {
            wx.hideNavigationBarLoading(), wx.hideLoading();
            var t = require("md5.js")(r);
            if ((t = wx.getStorageSync(t)) && t.data) return n.success && "function" == typeof n.success && n.success(t), 
            console.log("failreadcache:" + r), !0;
            n.fail && "function" == typeof n.fail && n.fail(e);
        },
        complete: function(e) {
            n.complete && "function" == typeof n.complete && n.complete(e);
        }
    });
}, util.getUserInfo = function(a) {
    function e() {
        console.log("start login");
        var n = {
            sessionid: "",
            wxInfo: "",
            memberInfo: ""
        };
        wx.login({
            success: function(e) {
                util.request({
                    url: "auth/session/openid",
                    data: {
                        code: e.code
                    },
                    cachetime: 0,
                    showLoading: !1,
                    success: function(t) {
                        t.data.errno || wx.getUserInfo({
                            success: function(e) {
                                n.sessionid = t.data.data.sessionid, n.wxInfo = e.userInfo, wx.setStorageSync("userInfo", n), 
                                util.request({
                                    url: "auth/session/userinfo",
                                    data: {
                                        signature: e.signature,
                                        rawData: e.rawData,
                                        iv: e.iv,
                                        encryptedData: e.encryptedData
                                    },
                                    method: "POST",
                                    header: {
                                        "content-type": "application/x-www-form-urlencoded"
                                    },
                                    cachetime: 0,
                                    success: function(e) {
                                        e.data.errno || (n.memberInfo = e.data.data, wx.setStorageSync("userInfo", n)), 
                                        "function" == typeof a && a(n);
                                    }
                                });
                            },
                            fail: function(e) {
                                var t = (t = getCurrentPages())[t.length - 1];
                                e.errMsg ? t.setData({
                                    showLoginModal: !0
                                }) : "function" == typeof a && a(n);
                            },
                            complete: function() {}
                        });
                    }
                });
            },
            fail: function() {
                wx.showModal({
                    title: "获取信息失败",
                    content: "请允许授权以便为您提供给服务",
                    confirmColor: "#f43f6c",
                    cancelColor: "#f43f6c",
                    success: function(e) {
                        e.confirm && util.getUserInfo();
                    }
                });
            }
        });
    }
    var t = wx.getStorageSync("userInfo");
    t.sessionid ? wx.checkSession({
        success: function() {
            "function" == typeof a && a(t);
        },
        fail: function() {
            t.sessionid = "", console.log("relogin"), wx.removeStorageSync("userInfo"), e();
        }
    }) : e();
}, util.navigateBack = function(t) {
    var e, n = t.delta || 1;
    t.data && ((e = (e = getCurrentPages())[e.length - (n + 1)]).pageForResult ? e.pageForResult(t.data) : e.setData(t.data)), 
    wx.navigateBack({
        delta: n,
        success: function(e) {
            "function" == typeof t.success && t.success(e);
        },
        fail: function(e) {
            "function" == typeof t.fail && t.fail(e);
        },
        complete: function() {
            "function" == typeof t.complete && t.complete();
        }
    });
}, util.footer = function(e) {
    var t, e = e, n = getApp().tabBar;
    for (t in n.list) n.list[t].pageUrl = n.list[t].pagePath.replace(/(\?|#)[^"]*/g, "");
    e.setData({
        tabBar: n,
        "tabBar.thisurl": e.__route__
    });
}, util.message = function(e, t, n) {
    if (!e) return !0;
    var a, r, o;
    "object" == (void 0 === e ? "undefined" : _typeof(e)) && (t = e.redirect, n = e.type, 
    e = e.title), t && (a = t.substring(0, 9), o = r = "", "navigate:" == a ? (o = "navigateTo", 
    r = t.substring(9)) : "redirect:" == a ? (o = "redirectTo", r = t.substring(9)) : (r = t, 
    o = "redirectTo")), console.log(r), "success" == (n = n || "success") ? wx.showToast({
        title: e,
        icon: "success",
        duration: 2e3,
        mask: !!r,
        complete: function() {
            r && setTimeout(function() {
                wx[o]({
                    url: r
                });
            }, 1800);
        }
    }) : "error" == n && wx.showModal({
        title: "系统信息",
        content: e,
        showCancel: !1,
        confirmColor: "#f43f6c",
        cancelColor: "#f43f6c",
        confirmText: "我知道了",
        complete: function() {
            r && ("back" == r ? wx.navigateBack() : "index" == r ? wx.switchTab({
                url: "/first_duoduoke/pages/index/index"
            }) : "task" == r ? wx.switchTab({
                url: "/first_duoduoke/pages/task/task"
            }) : wx[o]({
                url: r
            }));
        }
    });
}, util.user = util.getUserInfo, util.showLoading = function() {
    wx.getStorageSync("isShowLoading") && (wx.hideLoading(), wx.setStorageSync("isShowLoading", !1)), 
    wx.showLoading({
        title: "加载中",
        complete: function() {
            wx.setStorageSync("isShowLoading", !0);
        },
        fail: function() {
            wx.setStorageSync("isShowLoading", !1);
        }
    });
}, util.showImage = function(e) {
    if (!(e = e ? e.currentTarget.dataset.preview : "")) return !1;
    wx.previewImage({
        urls: [ e ]
    });
}, util.parseContent = function(e) {
    if (!e) return e;
    var t = e.match(new RegExp([ "\ud83c[\udf00-\udfff]", "\ud83d[\udc00-\ude4f]", "\ud83d[\ude80-\udeff]" ].join("|"), "g"));
    if (t) for (var n in t) e = e.replace(t[n], "[U+" + t[n].codePointAt(0).toString(16).toUpperCase() + "]");
    return e;
}, util.date = function() {
    this.isLeapYear = function(e) {
        return 0 == e.getYear() % 4 && (e.getYear() % 100 != 0 || e.getYear() % 400 == 0);
    }, this.dateToStr = function(e, t) {
        return e = arguments[0] || "yyyy-MM-dd HH:mm:ss", t = arguments[1] || new Date(), 
        (e = (e = (e = (e = (e = (e = (e = (e = (e = (e = (e = (e = e.replace(/yyyy|YYYY/, t.getFullYear())).replace(/yy|YY/, 9 < t.getYear() % 100 ? (t.getYear() % 100).toString() : "0" + t.getYear() % 100)).replace(/MM/, 9 <= t.getMonth() ? t.getMonth() + 1 : "0" + (t.getMonth() + 1))).replace(/M/g, t.getMonth())).replace(/w|W/g, [ "日", "一", "二", "三", "四", "五", "六" ][t.getDay()])).replace(/dd|DD/, 9 < t.getDate() ? t.getDate().toString() : "0" + t.getDate())).replace(/d|D/g, t.getDate())).replace(/hh|HH/, 9 < t.getHours() ? t.getHours().toString() : "0" + t.getHours())).replace(/h|H/g, t.getHours())).replace(/mm/, 9 < t.getMinutes() ? t.getMinutes().toString() : "0" + t.getMinutes())).replace(/m/g, t.getMinutes())).replace(/ss|SS/, 9 < t.getSeconds() ? t.getSeconds().toString() : "0" + t.getSeconds())).replace(/s|S/g, t.getSeconds());
    }, this.dateAdd = function(e, t, n) {
        switch (n = arguments[2] || new Date(), e) {
          case "s":
            return new Date(n.getTime() + 1e3 * t);

          case "n":
            return new Date(n.getTime() + 6e4 * t);

          case "h":
            return new Date(n.getTime() + 36e5 * t);

          case "d":
            return new Date(n.getTime() + 864e5 * t);

          case "w":
            return new Date(n.getTime() + 6048e5 * t);

          case "m":
            return new Date(n.getFullYear(), n.getMonth() + t, n.getDate(), n.getHours(), n.getMinutes(), n.getSeconds());

          case "y":
            return new Date(n.getFullYear() + t, n.getMonth(), n.getDate(), n.getHours(), n.getMinutes(), n.getSeconds());
        }
    }, this.dateDiff = function(e, t, n) {
        switch (e) {
          case "s":
            return parseInt((n - t) / 1e3);

          case "n":
            return parseInt((n - t) / 6e4);

          case "h":
            return parseInt((n - t) / 36e5);

          case "d":
            return parseInt((n - t) / 864e5);

          case "w":
            return parseInt((n - t) / 6048e5);

          case "m":
            return n.getMonth() + 1 + 12 * (n.getFullYear() - t.getFullYear()) - (t.getMonth() + 1);

          case "y":
            return n.getFullYear() - t.getFullYear();
        }
    }, this.strToDate = function(dateStr) {
        var data = dateStr, reCat = /(\d{1,4})/gm, t = data.match(reCat);
        return t[1] = t[1] - 1, eval("var d = new Date(" + t.join(",") + ");"), d;
    }, this.strFormatToDate = function(e, t) {
        var n = 0, a = -1, r = t.length;
        -1 < (a = e.indexOf("yyyy")) && a < r && (n = t.substr(a, 4));
        var o = 0;
        -1 < (a = e.indexOf("MM")) && a < r && (o = parseInt(t.substr(a, 2)) - 1);
        var i = 0;
        -1 < (a = e.indexOf("dd")) && a < r && (i = parseInt(t.substr(a, 2)));
        var s = 0;
        (-1 < (a = e.indexOf("HH")) || 1 < (a = e.indexOf("hh"))) && a < r && (s = parseInt(t.substr(a, 2)));
        var u = 0;
        -1 < (a = e.indexOf("mm")) && a < r && (u = t.substr(a, 2));
        var c = 0;
        return -1 < (a = e.indexOf("ss")) && a < r && (c = t.substr(a, 2)), new Date(n, o, i, s, u, c);
    }, this.dateToLong = function(e) {
        return e.getTime();
    }, this.longToDate = function(e) {
        return new Date(e);
    }, this.isDate = function(e, t) {
        null == t && (t = "yyyyMMdd");
        var n = t.indexOf("yyyy");
        if (-1 == n) return !1;
        var a = e.substring(n, n + 4);
        return -1 != (n = t.indexOf("MM")) && (n = e.substring(n, n + 2), -1 != (t = t.indexOf("dd")) && (t = e.substring(t, t + 2), 
        !(!isNumber(a) || "2100" < a || a < "1900" || !isNumber(n) || "12" < n || n < "01" || t > getMaxDay(a, n) || t < "01")));
    }, this.getMaxDay = function(e, t) {
        return 4 == t || 6 == t || 9 == t || 11 == t ? "30" : 2 == t ? e % 4 == 0 && e % 100 != 0 || e % 400 == 0 ? "29" : "28" : "31";
    }, this.isNumber = function(e) {
        return /^\d+$/g.test(e);
    }, this.toArray = function(e) {
        e = arguments[0] || new Date();
        var t = Array();
        return t[0] = e.getFullYear(), t[1] = e.getMonth(), t[2] = e.getDate(), t[3] = e.getHours(), 
        t[4] = e.getMinutes(), t[5] = e.getSeconds(), t;
    }, this.datePart = function(e, t) {
        t = arguments[1] || new Date();
        var n = "";
        switch (e) {
          case "y":
            n = t.getFullYear();
            break;

          case "M":
            n = t.getMonth() + 1;
            break;

          case "d":
            n = t.getDate();
            break;

          case "w":
            n = [ "日", "一", "二", "三", "四", "五", "六" ][t.getDay()];
            break;

          case "ww":
            n = t.WeekNumOfYear();
            break;

          case "h":
            n = t.getHours();
            break;

          case "m":
            n = t.getMinutes();
            break;

          case "s":
            n = t.getSeconds();
        }
        return n;
    }, this.maxDayOfDate = function(e) {
        return (e = arguments[0] || new Date()).setDate(1), e.setMonth(e.getMonth() + 1), 
        e = e.getTime() - 864e5, new Date(e).getDate();
    };
}, module.exports = util;